//
//  ArticleViewController.m
//  JiuLe
//
//  Created by 黄鑫 on 2016/10/20.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "ArticleViewController.h"
#import "BaseDataRequest.h"
#import "ArticleDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "CLUIBaseTableViewController+Empty.h"

@interface ArticleViewController ()
@property (strong, nonatomic) NSMutableArray *arrDataSource;
@property (assign, nonatomic) NSInteger page;
@end

@implementation ArticleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"文章订阅";
    [self showNavgationWithLeftItemWithTitle:@"首页"];
    self.page = 1;
    __weak typeof(self) weakSelf = self;
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        if (_arrDataSource.count == 0) {
            weakSelf.page = 1;
        }else{
            weakSelf.page ++;
        }
        [weakSelf getArticleList];
    }];
    [SVProgressHUD showWithStatus:@"获取文章列表.." maskType:SVProgressHUDMaskTypeClear];
    [self getArticleList];
}

- (void)getArticleList{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:[NSNumber numberWithInteger:self.page] forKey:@"page"];
    WS(weakSelf);
    [[BaseDataRequest alloc] getArticleList:dict withBlock:^(id responseObject, NSString *status, NSError *error) {
        [SVProgressHUD dismiss];
        [self.tableView.footer endRefreshing];
        if ([status isEqualToString:@"0"]) {
            NSMutableArray *arrResult = [NSMutableArray arrayWithArray:[responseObject objectForKey:@"result"]];
            if (arrResult.count == 0 || arrResult.count < 10) {
                [self.tableView.footer noticeNoMoreData];
            }
            [self.arrDataSource addObjectsFromArray:arrResult];
            [self.tableView reloadData];
        }else{
            if (self.tableView.footer.isRefreshing) {
                if (self.page>1) {
                    self.page--;
                }
            }
            if(self.arrDataSource.count == 0){
                [weakSelf showLabelEmptyCate:CGRectMake((SCREEN_WIDTH - 100)/2, (SCREEN_HEIGHT -140 -64)/2, 100, 100) withTitle:@"出错了，稍后再试吧" withImage:[UIImage imageNamed:@"icon_wifi"]];
                self.tableView.footer.hidden = YES;
            }else{
                if (error != nil) {
                    TIPSFORVIEW(@"请检测网络设置");
                }else{
                    TIPSFORVIEW([responseObject objectForKey:@"message"]);
                }
            }
        }
    }];
}

#pragma  mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrDataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = @"ArticleViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ;
    }
    UIImageView *imageV = [cell viewWithTag:1];
    UILabel *labName = [cell viewWithTag:2];
    UILabel *labFrom = [cell viewWithTag:3];
    UILabel *labTime = [cell viewWithTag:4];
    NSMutableDictionary *dicr = _arrDataSource[indexPath.row];
    [imageV sd_setImageWithURL:[NSURL URLWithString:[dicr objectForKey:@"thumbimage"]] placeholderImage:[UIImage imageNamed:@"iocn_default"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if(image == nil){
            imageV.image = [UIImage imageNamed:@"icon_default"];
        }
    }];
    labFrom.text= [NSString stringWithFormat:@"文章来源：%@",[dicr objectForKey:@"sourcename"]];
    labName.text = [dicr objectForKey:@"title"];
    labTime.text = [dicr objectForKey:@"createtime"];
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSMutableDictionary *dicr = _arrDataSource[indexPath.row];
    ArticleDetailViewController *articleDetail = [[UIStoryboard storyboardWithName:@"ArticleViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"ArticleDetailViewController"];
    articleDetail.strArticleURL = [dicr objectForKey:@"href"];
    articleDetail.strArticleID = [dicr objectForKey:@"id"];
    [self.navigationController pushViewController:articleDetail animated:YES];
}

- (NSMutableArray *)arrDataSource{
    if (_arrDataSource == nil) {
        _arrDataSource = [NSMutableArray array];
    }
    return _arrDataSource;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
